package com.sidianzhong.sdz.service.impl;
import com.github.pagehelper.PageHelper;
import com.sidianzhong.sdz.mapper.*;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.utils.PageInfo;
import com.sidianzhong.sdz.utils.Tools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by hxgqh on 2016/1/7.
*/
@Service
@Transactional
public class UserServiceImpl implements UserService{
    protected static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);
    // 将所有的modelMapper注入
        @Autowired
    private UserMapper userMapper;

    @Override
    public User create(User item) {
         userMapper.insert(item);
         return item;
    }

    @Override
    public int delete(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(User item) {
        return userMapper.updateByPrimaryKeySelective(item);
    }

    @Override
    public User get(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }
    @Override
    public PageInfo<User> getListWithPaging(Integer pageNum, Integer pageSize,
                                                  String sortItem, String sortOrder,User item){
       UserExample  example = new UserExample();
       UserExample.Criteria criteria = example.createCriteria();
       if(item.getUserName() != null ){
            criteria.andUserNameEqualTo(item.getUserName());
        }
        if(item.getHeadPortrait() != null ){
            criteria.andHeadPortraitEqualTo(item.getHeadPortrait());
        }
        if(item.getInvitationCode() != null ){
            criteria.andInvitationCodeEqualTo(item.getInvitationCode());
        }
        if(item.getInviter() != null ){
            criteria.andInviterEqualTo(item.getInviter());
        }
        if(item.getPhone() != null ){
            criteria.andPhoneEqualTo(item.getPhone());
        }
        if(item.getArea() != null ){
            criteria.andAreaEqualTo(item.getArea());
        }
        if(item.getLoginPassword() != null ){
            criteria.andLoginPasswordEqualTo(item.getLoginPassword());
        }
        if(item.getPayPassword() != null ){
            criteria.andPayPasswordEqualTo(item.getPayPassword());
        }
        if(item.getLoginNum() != null ){
            criteria.andLoginNumEqualTo(item.getLoginNum());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<User> list = this.userMapper.selectByExample(example);
       PageInfo result = new PageInfo(list);
       return result;
    }

    @Override
    public List<User> getList(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,User item){
       UserExample  example = new UserExample();
       UserExample.Criteria criteria = example.createCriteria();
       if(item.getUserName() != null ){
            criteria.andUserNameEqualTo(item.getUserName());
        }
        if(item.getHeadPortrait() != null ){
            criteria.andHeadPortraitEqualTo(item.getHeadPortrait());
        }
        if(item.getInvitationCode() != null ){
            criteria.andInvitationCodeEqualTo(item.getInvitationCode());
        }
        if(item.getInviter() != null ){
            criteria.andInviterEqualTo(item.getInviter());
        }
        if(item.getPhone() != null ){
            criteria.andPhoneEqualTo(item.getPhone());
        }
        if(item.getArea() != null ){
            criteria.andAreaEqualTo(item.getArea());
        }
        if(item.getLoginPassword() != null ){
            criteria.andLoginPasswordEqualTo(item.getLoginPassword());
        }
        if(item.getPayPassword() != null ){
            criteria.andPayPasswordEqualTo(item.getPayPassword());
        }
        if(item.getLoginNum() != null ){
            criteria.andLoginNumEqualTo(item.getLoginNum());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
       example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
       PageHelper.startPage(pageNum, pageSize);
       List<User> list = this.userMapper.selectByExample(example);
       return list;
    }

     @Override
     public PageInfo<JSONObject> getListWithObject(Integer pageNum, Integer pageSize,
                                                      String sortItem, String sortOrder,User item){
        UserExample  example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        if(item.getUserName() != null ){
            criteria.andUserNameEqualTo(item.getUserName());
        }
        if(item.getHeadPortrait() != null ){
            criteria.andHeadPortraitEqualTo(item.getHeadPortrait());
        }
        if(item.getInvitationCode() != null ){
            criteria.andInvitationCodeEqualTo(item.getInvitationCode());
        }
        if(item.getInviter() != null ){
            criteria.andInviterEqualTo(item.getInviter());
        }
        if(item.getPhone() != null ){
            criteria.andPhoneEqualTo(item.getPhone());
        }
        if(item.getArea() != null ){
            criteria.andAreaEqualTo(item.getArea());
        }
        if(item.getLoginPassword() != null ){
            criteria.andLoginPasswordEqualTo(item.getLoginPassword());
        }
        if(item.getPayPassword() != null ){
            criteria.andPayPasswordEqualTo(item.getPayPassword());
        }
        if(item.getLoginNum() != null ){
            criteria.andLoginNumEqualTo(item.getLoginNum());
        }
        if(item.getStatus() != null ){
            criteria.andStatusEqualTo(item.getStatus());
        }
        if(item.getCreateTime() != null ){
            criteria.andCreateTimeEqualTo(item.getCreateTime());
        }
        if(item.getLastUpdateTime() != null ){
            criteria.andLastUpdateTimeEqualTo(item.getLastUpdateTime());
        }
        example.setOrderByClause(Tools.humpToLine(sortItem)+" "+sortOrder);
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = this.userMapper.selectByExample(example);
        List<JSONObject> lists =  list.stream().map(user -> JSONObject.parseObject(JSONObject.toJSONString(user))).collect(Collectors.toList());
        PageInfo result = new PageInfo(lists);
        return result;
        }

    @Override
    public User getUserByPhone(String phone) {
        User user = this.userMapper.selectUserByPhone(phone);
        return user;
    }

    @Override
    public User getUserByArea(String phone, String area) {
        User user = this.userMapper.getUserByArea(phone,area);
        return user;
    }

    @Override
    public User getUserByCode(String code) {
        UserExample  example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andInvitationCodeEqualTo(code);
        List<User> list = this.userMapper.selectByExample(example);
        if (list.size()>0){
            return list.get(0);
        }
        return null;
    }

}
